<template>
  <div class="login-container">
    <a-card title="高铁内饰管理系统" class="card">
      <a-form :label-col="{ span: 8 }" :model="formState" :wrapper-col="{ span: 16 }" @finish="onFinish"
        @finishFailed="onFinishFailed" autocomplete="off" name="basic">
        <a-form-item :rules="[{ required: true, message: 'Please input your username!' }]" label="用户名" name="username">
          <a-input v-model:value="formState.username" />
        </a-form-item>

        <a-form-item label="密码" name="password" :rules="[{ required: true, message: 'Please input your password!' }]">
          <a-input-password v-model:value="formState.password" />
        </a-form-item>

        <a-form-item :wrapper-col="{ offset: 8, span: 16 }">
          <a-button type="primary" html-type="submit">登录</a-button>
        </a-form-item>
      </a-form>
    </a-card>
  </div>
</template>


<script setup>
import { reactive } from 'vue'
import { useRouter } from 'vue-router'
import axios from 'axios'
import { useSystemStore } from '@/store/systemStore'
import store from "store";
const router = useRouter()
const systemStore = useSystemStore()

const formState = reactive({
  username: '',
  password: '',
})

const onFinish = async (data) => {
  const result = await axios({
    url: '/api/sysuser/login',
    method: 'post',
    data
  })
  const token = result.data.data
  systemStore.setToken(token)
  const r2 = await axios({
    url: '/api/syspermission/perms',
    method: 'get',
    headers:{
      access_token: token
    }
  })
  systemStore.setPerms(r2.data.data)

  router.push('/layouts/welcome')
}

const onFinishFailed = errorInfo => {
  console.log('Failed:', errorInfo)
}
</script>

<style lang="less" scoped>
.login-container {
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;

  .card {
    width: 400px;
  }
}
</style>
